package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.ChanPinXinXiErDaoImpl;
import com.ysd.entity.ChanPinXinXi;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminCPXinXiServlet extends BaseServlet {
	/**
	 * 商品多条件查询
	 * localhost:8080/shangcheng/adminCPXinXi?reqName=getCPXinxi
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getCPXinxi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//接收前台传来的参数
		int cPId=0;
		if(request.getParameter("cPId")!=null && !request.getParameter("cPId").equals("")) {
			cPId=Integer.parseInt(request.getParameter("cPId"));//numberFormatExcption数值格式化异常
		}
		String tuPian=request.getParameter("tuPian");
		String cPName=request.getParameter("cPName");
		int bengincPShouJia=0;
		if(request.getParameter("bengincPShouJia")!=null && !request.getParameter("bengincPShouJia").equals("")) {
			bengincPShouJia=Integer.parseInt(request.getParameter("bengincPShouJia"));
		}
		int endcPShouJia=0;
		if(request.getParameter("endcPShouJia")!=null && !request.getParameter("endcPShouJia").equals("")) {
			endcPShouJia=Integer.parseInt(request.getParameter("endcPShouJia"));
		}
		String pinPai=request.getParameter("pinPai");
		String shiFouDZ=request.getParameter("shiFouDZ");
		String xQTpp=request.getParameter("xQTpp");
		String xQTp=request.getParameter("xQTp");
		String chanPinYanSe=request.getParameter("chanPinYanSe");
		int fenLeiID=0;
		if(request.getParameter("fenLeiID")!=null && !request.getParameter("fenLeiID").equals("")) {
			fenLeiID=Integer.parseInt(request.getParameter("fenLeiID"));
		}
		int page=0;
		int size=0;
		if(request.getParameter("page")!=null && !request.getParameter("page").equals("")) {
			page=Integer.parseInt(request.getParameter("page"));
		}
		if(request.getParameter("size")!=null && !request.getParameter("size").equals("")) {
			size=Integer.parseInt(request.getParameter("size"));
		}
		ChanPinXinXi x=new ChanPinXinXi(cPId, tuPian, cPName, 0, bengincPShouJia, endcPShouJia, pinPai, shiFouDZ, xQTpp, xQTp, chanPinYanSe, fenLeiID, "");
		
		ChanPinXinXiErDaoImpl xxDao=new ChanPinXinXiErDaoImpl();
		List<ChanPinXinXi> xxList = xxDao.getChanPinTJ(x, page, size);
		
		Gson gson=new Gson();
		String res=gson.toJson(xxList);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		out.print(res);
		
		out.flush();
		out.close();
	}
	/**
	 * 商品多条件查询
	 * localhost:8080/shangcheng/adminCPXinXi?reqName=getCPXinxiSize
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getCPXinxiSize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//接收前台传来的参数
		int cPId=0;
		if(request.getParameter("cPId")!=null && !request.getParameter("cPId").equals("")) {
			cPId=Integer.parseInt(request.getParameter("cPId"));//numberFormatExcption数值格式化异常
		}
		String tuPian=request.getParameter("tuPian");
		String cPName=request.getParameter("cPName");
		int bengincPShouJia=0;
		if(request.getParameter("bengincPShouJia")!=null && !request.getParameter("bengincPShouJia").equals("")) {
			bengincPShouJia=Integer.parseInt(request.getParameter("bengincPShouJia"));
		}
		int endcPShouJia=0;
		if(request.getParameter("endcPShouJia")!=null && !request.getParameter("endcPShouJia").equals("")) {
			endcPShouJia=Integer.parseInt(request.getParameter("endcPShouJia"));
		}
		String pinPai=request.getParameter("pinPai");
		String shiFouDZ=request.getParameter("shiFouDZ");
		String xQTpp=request.getParameter("xQTpp");
		String xQTp=request.getParameter("xQTp");
		String chanPinYanSe=request.getParameter("chanPinYanSe");
		int fenLeiID=0;
		if(request.getParameter("fenLeiID")!=null && !request.getParameter("fenLeiID").equals("")) {
			fenLeiID=Integer.parseInt(request.getParameter("fenLeiID"));
		}
		
		ChanPinXinXi x=new ChanPinXinXi(cPId, tuPian, cPName, 0, bengincPShouJia, endcPShouJia, pinPai, shiFouDZ, xQTpp, xQTp, chanPinYanSe, fenLeiID, "");
		
		ChanPinXinXiErDaoImpl xxDao=new ChanPinXinXiErDaoImpl();
		List<ChanPinXinXi> xxList = xxDao.getChanPinSize(x);
		
		Gson gson=new Gson();
		String res=gson.toJson(xxList.size());
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	/**删除
	 * localhost:8080/shangcheng/adminCPXinXi?reqName=delCPShanchu
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delCPShanchu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int cPId=Integer.parseInt(request.getParameter("cpId"));
		
		request.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		ChanPinXinXiErDaoImpl scDao=new ChanPinXinXiErDaoImpl();
		int n=scDao.delCPShanChu(cPId);
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除成功",""));
		}
		
		out.flush();
		out.close();
	}
	/**
	 * 批量删除
	 * localhost:8080/shangcheng/adminCPXinXi?reqName=deletecPIds
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deletecPIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cPIds = request.getParameter("cPIds");
		
		request.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		ChanPinXinXiErDaoImpl scDao=new ChanPinXinXiErDaoImpl();
		int n=scDao.deletecPIds(cPIds);
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除成功",""));
		}
		
		out.flush();
		out.close();
	}
	

	/**
	 * 添加产品
	 * localhost:8080/shangcheng/adminCPXinXi?reqName=addTJ
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addTJ(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置字符集编码格式
		PrintWriter out=response.getWriter();//out对象
		ChanPinXinXiErDaoImpl cpxxDao=new ChanPinXinXiErDaoImpl();
		
		String tuPian=request.getParameter("cpImg");
		String cPName=request.getParameter("cPName");
		int cPShouJia=0;
		if(request.getParameter("cPShouJia")!=null && !request.getParameter("cPShouJia").equals("")) {
			cPShouJia=Integer.parseInt(request.getParameter("cPShouJia"));
		}
		String pinPai=request.getParameter("pinPai");
		String shiFouDZ=request.getParameter("shiFouDZ");
		String xQTpp=request.getParameter("xQTpp");
		String xQTp=request.getParameter("xQTp");
		String chanPinYanSe=request.getParameter("chanPinYanSe");
		int fenLeiID=0;
		if(request.getParameter("fenLeiID")!=null && !request.getParameter("fenLeiID").equals("")) {
			fenLeiID=Integer.parseInt(request.getParameter("fenLeiID"));
		}
		
		if(cPName!=null && !cPName.equals("")) {
			List<ChanPinXinXi> cpxxList=cpxxDao.getCpName(cPName);
			if(cpxxList.size()==0) {
				ChanPinXinXi xx=new ChanPinXinXi(0, tuPian, cPName, cPShouJia, pinPai, shiFouDZ, xQTpp, xQTp, chanPinYanSe, fenLeiID);
				System.out.println(xx);
				int n=cpxxDao.addCpXinXi(xx);
				
				if(n>0) {
					out.print(Result.toClient(true, "添加成功", ""));
				}else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			}else {
				out.print(Result.toClient(false, "该产品已存在", ""));
			}
		}
		out.flush();
		out.close();
		
	}
	/**
	 * 添加图片
	 * localhost:8080/shangcheng/adminCPXinXi?reqName=upload
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out= response.getWriter();//out对象
		
		//创建smartUpload对象
		SmartUpload smart=new SmartUpload();
		//设置编码格式
		smart.setCharset("UTF-8");
		//初始化
		smart.initialize(getServletConfig(),request,response);
		
		try {
			//上传
			smart.upload();
			//设置上传文件名
			String fileName=System.currentTimeMillis()+".jpg";
			//将文件保存到指定目录
			smart.getFiles().getFile(0).saveAs("images/"+fileName);
			//响应数据
			out.print(Result.toClient(true, "上传成功",fileName));
		}catch(SmartUploadException e){
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败", "图片过大"));
		}
		out.flush();
		out.close();
	}
	
	/**
	 * 数据回显
	 * localhost:8080/shangcheng/adminCPXinXi?reqName=getXXid
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getXXid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		ChanPinXinXiErDaoImpl xxDao=new ChanPinXinXiErDaoImpl();
		int xxid=0;
		if(request.getParameter("cPId")!=null && !request.getParameter("cPId").equals("")) {
			xxid=Integer.parseInt(request.getParameter("cPId"));
		}
		ChanPinXinXi x=xxDao.getXinXiId(xxid);
		
		Gson gson=new Gson();
		String res=gson.toJson(x);
		out.print(res);
		
		out.flush();
		out.close();
	}
	/**
	 * 修改产品信息
	 * localhost:8080/shangcheng/adminCPXinXi?reqName=updateXinXi
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateXinXi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ChanPinXinXiErDaoImpl xxDao=new ChanPinXinXiErDaoImpl();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		int cPId=0;
		if(request.getParameter("cPId")!=null && !request.getParameter("cPId").equals("")) {
			cPId=Integer.parseInt(request.getParameter("cPId"));
		}
		
		String tuPian=request.getParameter("tuPian");
		String cPName=request.getParameter("cpname");
		int cPShouJia=0;
		if(request.getParameter("shoujia")!=null && !request.getParameter("shoujia").equals("")) {
			cPShouJia=Integer.parseInt(request.getParameter("shoujia"));
		}
		String pinPai=request.getParameter("pinpai");
		String shiFouDZ=request.getParameter("dz");
		String xQTpp=request.getParameter("xQTpp");
		String xQTp=request.getParameter("xQTp");
		String chanPinYanSe=request.getParameter("ys");
		int fenLeiID=0;
		if(request.getParameter("fl")!=null && !request.getParameter("fl").equals("")) {
			fenLeiID=Integer.parseInt(request.getParameter("fl"));
		}
		if(cPName!=null && !cPName.equals("")) {
			List<ChanPinXinXi> xxList=xxDao.getCpName(cPName);
			
				ChanPinXinXi d=new ChanPinXinXi(cPId, tuPian, cPName, cPShouJia, pinPai, shiFouDZ, xQTpp, xQTp, chanPinYanSe, fenLeiID);
				int n=xxDao.updateCPid(d);
				if(n>0) {
					out.print(Result.toClient(true,"修改成功",""));
				}else {
					out.print(Result.toClient(false,"修改失败",""));
				}
			
		}
		out.flush();
		out.close();
		
	}
}
